package edu.nf.relaxenjoy.service;

import com.rabbitmq.client.Channel;
import edu.nf.relaxenjoy.common.constant.platform.RabbitMqConstant;
import edu.nf.relaxenjoy.domain.entity.Album;
import edu.nf.relaxenjoy.domain.entity.Comment;
import edu.nf.relaxenjoy.domain.entity.Post;
import edu.nf.relaxenjoy.domain.entity.User;
import edu.nf.relaxenjoy.mapper.AlbumMapper;
import edu.nf.relaxenjoy.mapper.CommentMapper;
import edu.nf.relaxenjoy.mapper.PostMapper;
import edu.nf.relaxenjoy.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author zhu
 * @date 2024-12-16
 */
@Component
@RequiredArgsConstructor
public class PlatformConsumer {

    private final PostMapper postMapper;

    private final UserMapper userMapper;

    private final AlbumMapper albumMapper;

    private final CommentMapper commentMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqConstant.POST_STATE_QUEUE, durable = "true"),
            exchange = @Exchange(value = RabbitMqConstant.POST_STATE_EXCHANGE),
            key = {RabbitMqConstant.POST_STATE_KEY}
    ))
    public void imgDetailStateListener(Post post, Message message, Channel channel) throws IOException {
        postMapper.updateById(post);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqConstant.USER_STATE_QUEUE, durable = "true"),
            exchange = @Exchange(value = RabbitMqConstant.USER_STATE_EXCHANGE),
            key = {RabbitMqConstant.USER_STATE_KEY}
    ))
    public void userStateListener(User user, Message message, Channel channel) throws IOException {
        userMapper.updateById(user);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqConstant.COMMENT_STATE_QUEUE, durable = "true"),
            exchange = @Exchange(value = RabbitMqConstant.COMMENT_STATE_EXCHANGE),
            key = {RabbitMqConstant.COMMENT_STATE_KEY}
    ))
    public void commentStateListener(Comment comment, Message message, Channel channel) throws IOException {
        commentMapper.updateById(comment);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMqConstant.ALBUM_STATE_QUEUE, durable = "true"),
            exchange = @Exchange(value = RabbitMqConstant.ALBUM_STATE_EXCHANGE),
            key = {RabbitMqConstant.ALBUM_STATE_KEY}
    ))
    public void albumStateListener(Album album, Message message, Channel channel) throws IOException {
        albumMapper.updateById(album);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
